/**
 *
 * @file
 *              heap_petrov.h
 *
 * @brief
 *              declaration of heap class
 *
 * @author
 *              petrov
 */

 /*
 Bitmapped memory manager. Class heap_petrov encapsulates allocator's functionality
 struct memblock contains formated blocks of memory for user's pointer. Type - linked list
 */

#ifndef __HEAP_PETROV_H__
#define __HEAP_PETROV_H__

#include "heap.h"
#include <map>
#include <stdlib.h>

#include <iostream>
#include <Windows.h>

using std::map;
using std::multimap;
using std::make_pair;
using std::pair;

class heap_petrov: public heap876 {
private:
	CRITICAL_SECTION cs;
	char* _heap;
	size_t alloc_size;
	HANDLE hMutex;;
	
	class section {
	public:
		size_t size;
		bool is_empty;
		multimap<size_t, char*>::iterator itr;

		section(size_t sz = 0, bool b = true, multimap<size_t, char*>::iterator i = 0) { size = sz; is_empty = b; itr = i; }
	};
	
	map<char*, section> M;
	multimap<size_t, char*> MM;
	
public:
        heap_petrov(size_t total, alloc_flags flag);
        ~heap_petrov();


        void* alloc(size_t sz, unsigned int tag = 0);
        void free(void* ptr);
		void free_all(unsigned int tag = 0);
		size_t get_occupancy() const { return alloc_size; }


        
        //debug function. prints information about content of mem_list
        void prt();
};

/*
special class for safe syncronization
*/

class SyncHelper{
public:
	SyncHelper(CRITICAL_SECTION& argcs): cs(argcs) { EnterCriticalSection(&cs);};
	~SyncHelper(){ LeaveCriticalSection(&cs);};
private:
	CRITICAL_SECTION cs;
};



#endif